package com.rede.didiok.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.admin.dao.ExceptionLogDao;
import com.rede.didiok.admin.entity.ExceptionLogEntity;
import com.rede.didiok.admin.service.ExceptionLogService;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.dto.admin.ExceptionLogDto;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.utils.DateUtils;
import com.rede.didiok.common.utils.StringUtils;


@Service("exceptionLogService")
public class ExceptionLogServiceImpl extends ServiceImpl<ExceptionLogDao, ExceptionLogEntity> implements ExceptionLogService {

    @Autowired
    private ExceptionLogService exceptionLogService;

    /***************************** admin:start **************************/
    // region  didiok-admin

    @Override
    public IPage<ExceptionLogEntity> getPageList(ExceptionLogDto exceptionLogDto) {
        QueryWrapper<ExceptionLogEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(exceptionLogDto.getKeyword())) {
            queryWrapper.like(SQLConf.CONTENT, exceptionLogDto.getKeyword());
        }

        if (!StringUtils.isEmpty(exceptionLogDto.getOperation())) {
            queryWrapper.like(SQLConf.OPERATION, exceptionLogDto.getOperation());
        }

        if (!StringUtils.isEmpty(exceptionLogDto.getStartTime())) {
            String[] time = exceptionLogDto.getStartTime().split(SysConf.FILE_SEGMENTATION);
            if (time.length == Constants.NUM_TWO) {
                queryWrapper.between(SQLConf.CREATE_TIME, DateUtils.str2Date(time[0]), DateUtils.str2Date(time[1]));
            }
        }

        Page<ExceptionLogEntity> page = new Page<>();
        page.setCurrent(exceptionLogDto.getCurrentPage());
        page.setSize(exceptionLogDto.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        queryWrapper.select(ExceptionLogEntity.class, i -> !i.getProperty().equals(SQLConf.EXCEPTION_JSON));
        IPage<ExceptionLogEntity> pageList = exceptionLogService.page(page, queryWrapper);
        return pageList;
    }

    // endregion
    /***************************** admin:end **************************/



    /***************************** web:start **************************/
    // region didiok-web

    // endregion
    /***************************** web:end **************************/

}